package com.acme;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class PlayerProfile extends Activity {

	private Player player;
	private PlayerProfileResponseReceiver receiver;
	
	public class PlayerProfileResponseReceiver extends BroadcastReceiver {
		   public static final String ACTION_RESP = "com.test.intent.action.PLAYER_PROFILE_RECV";
		 
		   @Override
		    public void onReceive(Context context, Intent intent) {
		       	String response = intent.getStringExtra(FootballStatsService.GET_PLAYER_OUT_JSON);
		       	player = Player.getPlayer(response);

		       	TextView playerName = (TextView) findViewById(R.id.player_name);
		       	playerName.setText(player.firstName+" "+player.lastName);
		       	
		       	TextView nickname = (TextView) findViewById(R.id.nickname);
		       	if (!player.nickname.isEmpty()){
		       		nickname.setText("AKA "+player.nickname);
		       	} else {
		       		nickname.setText("");
		       	}
		       	
		       	TextView dob = (TextView) findViewById(R.id.dob);
		       	dob.setText(player.dob);
		       	
		       	TextView hometown = (TextView) findViewById(R.id.hometown);
		       	hometown.setText(player.hometown);
		       	
		       	TextView weight = (TextView) findViewById(R.id.weight);
		       	weight.setText(player.weight);
		       	
		       	TextView height = (TextView) findViewById(R.id.height);
		       	height.setText(player.height);
		       	
		       	TextView college = (TextView) findViewById(R.id.college);
		       	college.setText(player.college);
		       	
		       	TextView teams = (TextView) findViewById(R.id.teams);
		       	teams.setText(player.teams[0]);
		       	
		       	TextView drafted = (TextView) findViewById(R.id.drafter);
		       	drafted.setText(player.drafted);
		       	
		    }
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile);
		
		Intent intent = getIntent();
		String playerId = intent.getStringExtra(Player.PLAYER_ID);
		
		IntentFilter filter = new IntentFilter(PlayerProfileResponseReceiver.ACTION_RESP);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		receiver = new PlayerProfileResponseReceiver();
		registerReceiver(receiver, filter);

		Intent msgIntent = new Intent(this, FootballStatsService.class);
		msgIntent.putExtra(FootballStatsService.ACTION,FootballStatsService.ACTION_GET_PLAYER);
		msgIntent.putExtra(FootballStatsService.GET_PLAYER_IN_ID, playerId);
		startService(msgIntent);
		
	}
	
	public void onDestroy(){
		super.onDestroy();
		unregisterReceiver(receiver);
	}
}
